import type {
  ProjectSearchRequest,
  ResponseBaseBody,
} from "@/interface/project";
import type { DataflowFileEntityRes } from "@/interface/task";
import request from "@/utils/request";
const mock = "https://m1.apifoxmock.com/m1/7176471-6900810-default";

// 参考资料文件接口
export interface ReferenceFile {
  id: string | number;
  fileName: string;
  fileType: string;
  fileSize: number;
  uploadTime: string;
  uploader: string;
  description?: string;
  downloadUrl?: string;
  previewUrl?: string;
}

// 参考资料列表查询参数
export interface ReferenceListRequest {
  page?: number;
  pageSize?: number;
  keyword?: string;
  fileType?: string;
  uploader?: string;
  startTime?: string;
  endTime?: string;
}

// 参考资料列表响应
export interface ReferenceListResponse {
  list: ReferenceFile[];
  total: number;
  page: number;
  pageSize: number;
}

/**
 * 获取参考资料文件列表
 * @param params 查询参数
 * @returns 参考资料文件列表
 */
export const getReferenceFileList = (
  params?: ProjectSearchRequest,
): Promise<ResponseBaseBody<DataflowFileEntityRes>> => {
  return request({
    url: `${mock}/dataflowFile/list`,
    method: "POST",
    params,
  });
};

/**
 * 上传参考资料文件
 * @param file 文件对象
 * @param description 文件描述
 * @returns 上传结果
 */
export const uploadReferenceFile = (
  file: File,
  description?: string,
): Promise<ReferenceFile> => {
  const formData = new FormData();
  formData.append("file", file);
  if (description) {
    formData.append("description", description);
  }

  return request({
    url: "/dataflowFile/upload",
    method: "POST",
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
};

/**
 * 删除参考资料文件
 * @param id 文件ID
 * @returns 删除结果
 */
export const deleteReferenceFile = (id: string | number): Promise<void> => {
  return request({
    url: `/dataflowFile/${id}`,
    method: "DELETE",
  });
};

/**
 * 下载参考资料文件
 * @param id 文件ID
 * @returns 下载链接
 */
export const downloadReferenceFile = (id: string | number): Promise<string> => {
  return request({
    url: `/dataflowFile/${id}/download`,
    method: "GET",
  });
};
